import Vue from 'vue'
import VueRouter from 'vue-router';
import Login from "../components/Login";
import Main from "../view/Main";
import Home from "../view/home/Home";
import User from "../view/user/User";
import Good from "../view/goods/Good";
import Order from "../view/order/Order";
import Notice from "../view/notice/Notice";
import Type from "../view/goods/Type";
import Add from "../view/goods/Add";



const routes=[
    {
        path: '/',
        redirect:'/main'
    },
    {
        path:'/login',
        component: Login
    },
    {
        path: '/main',
        component: Main,
        redirect: '/home',
        children:[
            { path:'/home',name:'Home',component: Home},
            { path:'/user',name:'User',component: User},
            { path:'/good',name:'Good',component: Good},
            { path:'/order',name:'Order',component: Order},
            { path:'/notice',name:'Notice',component: Notice},
            { path: "/type",name: 'Type',component: Type},
            { path: "/goods/add",name: 'Add',component: Add}
        ]
    }
]

Vue.use(VueRouter)

const router=new VueRouter({
    routes,
    mode: 'history',
})

//挂载路由导航守卫
router.beforeEach((to,from,next)=>{
    if (to.path === '/login') return next()
    const tokenStr =window.sessionStorage.getItem("token")
    if (!tokenStr) return next('/login')
    next()
})

//路由导航冗余报错（路由重复）
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

export default router